package regiongenerator.data;

import org.lwjgl.opengl.GL11;

/**
 *
 * @author grimmbo
 */
public class Field {

    private int             altitude;
    private Type            type;
    private FieldConstraint constraint;

    public Field()
    {
        altitude = 0;
        type = Type.NONE;
        constraint = null;
    }

    public Field(int altitude)
    {
        this.altitude = altitude;
        type = Type.NONE;
        constraint = null;
    }
    
    public Field(Type type)
    {
        altitude = 0;
        this.type = type;
        constraint = null;
    }

    public Field(FieldConstraint constraint)
    {
    	altitude = 0;
    	type = Type.NONE;
        setConstraint(constraint);
    }

    
    /**
     * Draws the {@code Field} object onto the screen using OpenGL.
     * @param x screen coordinate on x-axis, in pixels
     * @param y screen coordinate on y-axis, in pixels
     */
    public void draw(double size)
    {
    	GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
    	
    	
    	//GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
        // set the color of the field (R,G,B) depending on field-type
    	switch (type){
        case NONE:
        	GL11.glColor3d(0, 0, 0);
            break;
        case SOIL:
            GL11.glColor3d(0.5, 0.3, 0.25);
            break;
        case ROCK:
        	GL11.glColor3d(0.8, 0.8, 0.8);
        	break;
        case RIVER_SOURCE:
        	GL11.glColor3d(0.2, 1, 0.2);
        	break;
        case RIVER_BED:
        	GL11.glColor3d(0.0, 0.5, 0.0);
        	break;
        default:
        	GL11.glColor3d(1, 1, 1);
        }
        
        GL11.glBegin(GL11.GL_QUAD_STRIP);
			GL11.glVertex3d(0, altitude*5, 0);
			GL11.glVertex3d(0, 0, 0);
			GL11.glVertex3d(size, altitude*5, 0);
			GL11.glVertex3d(size, 0, 0);
			GL11.glVertex3d(size, altitude*5, size);
			GL11.glVertex3d(size, 0, size);
			GL11.glVertex3d(0, altitude*5, size);
			GL11.glVertex3d(0, 0, size);
			GL11.glVertex3d(0, altitude*5, 0);
			GL11.glVertex3d(0, 0, 0);
		GL11.glEnd();
    }

    /**
     * @return the altitude
     */
    public int getAltitude() {
        return altitude;
    }

    /**
     * @param altitude the elevation to set
     */
    public void setAltitude(int altitude) {
        this.altitude = altitude;
    }

    /**
     * @return the type
     */
    public Type getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(Type type) {
        this.type = type;
    }

    /**
     * @return the constraint
     */
    public FieldConstraint getConstraint() {
        return constraint;
    }

    /**
     * @param constraint the constraint to set
     */
    public void setConstraint(FieldConstraint constraint) {
        if (this.constraint != null)
            this.constraint.addConstraint(constraint);
        else
            this.constraint = constraint;
    }
    
    public enum Type
    {
        /*
         *  For now, 4 types of fields
         */
    	
    	NONE,
    	SOIL,
    	ROCK,
    	RIVER_SOURCE,
    	RIVER_BED
    }
}
